<?php

function uiStdWidgetStartScript($class, $id){
	return <<<END
if(\$("$id"))
	new $class(\$("$id"));
else
	new $class(\$("{$_GET['$$ui-resultContainer']}"));
END;
}

/**** Base Tags ****/

class cHtmlCode extends cDycmsClientNode
/**
 * @desc
 * Represents raw HTML source code
 **/
{
	protected $mCode;

	function __construct($code)
	{
		$this->mCode = $code;
	}

	function DisplayBody()
	{
		echo($this->mCode);
	}

	function __toString()
	{
		return $this->mCode;
	}
}

class cHtmlWrapper extends cDycmsClientNode
/**
* @desc 
* Wraps its children with raw HTML
*/
{
	protected $msHeader;
	protected $msFooter;

	function __construct($s_h = "", $s_f = "")
	{
		$this->msHeader = $s_h;
		$this->msFooter = $s_f;
	}

	function DisplayHeader()
	{
		echo($this->msHeader);
	}

	function DisplayFooter()
	{
		echo($this->msFooter);
	}
}

class cHtmlWrapperTag extends cDycmsClientNode
/**
* @desc
* Wraps its children with a HTML tag.
*/
{
	protected $msTag;
	protected $mArgs;
	
	function __construct($sTag, $argv = array()){
		$this->msTag = $sTag;
		$this->mArgs = $argv;
	}
	
	function DisplayHeader(){
		echo '<'.$this->msTag.uiElAttributes($this->mArgs).'>';
	}
	
	function DisplayFooter(){
		echo "</{$this->msTag}>";
	}
}

class cHtmlTagWrap extends cHtmlWrapperTag
{
 	function __construct($text, $tag = 'div', $argv = array()){
 	 	parent::__construct($tag, $argv);
 	 	$this->AddChild($text);
 	}
}

class cHtmlWrap extends cHtmlWrapper
{
	function __construct($s_h, IDycmsDisplayable $r_child, $s_f = "")
	{
		parent::__construct($s_h, $s_f);
		$this->AddChild($r_child);
	}
}

/**
 * @class cUiCompletionMsg
 * 
 * Outputs a success message to the client and adds meta data
 * to the page so that the client side can display the message
 * without refreshing the page.
 */
class cUiCompletionMsg extends cHtmlWrapperTag
{
	function __construct($msg){
		parent::__construct('div', array(
			'class'=>'CompletionMsg'
		));
		
		$this->AddChild(new cUiIcon('tick', 'Completed'));
		$this->AddChild($msg);
		
		cUiPage::GetInst()->AddMetaData('status', 'Completion');
	}
}


/**** Basic Layout ****/

class cUiHeading extends cHtmlWrapperTag
{
	function __construct($text = null, $depth = 1, $argv = array()){
		parent::__construct('h'.intval($depth), $argv);
		if($text) $this->AddChild($text);
	} 
}

class cUiLayer extends cHtmlWrapperTag
{
	function __construct($argv = array()){
		if(!is_array($argv)) {
			$argv = array('class' => $argv);
		}
		parent::__construct('div', $argv);
	}
}

class cUiSeperator extends cDycmsClientNode
{
	function Display(){
		echo '<hr class="UiSeperator" />';
	}
}

class cUiSection extends cHtmlWrapperTag
{
	function __construct($header, $argv = array()){
		if(!isset($argv['class']))
			$argv['class'] = 'UiSection';
		
		parent::__construct('div', $argv);
		
		//
		// Create header
		//
		
		$argv['class'] = 'UiSectionHeader';
		if(isset($argv['id']))
			$argv['id'] .= '-header';
		
		$this->AddChild(new cHtmlTagWrap($header, 'span',
			$argv));
	}
}

class cUiModule extends cDycmsClientNode
{
	protected $msTitle;
	protected $msId;

	function __construct($s_title, $argv = array())
	{
		$this->msTitle = $s_title;
		$this->msId = isset($argv['id']) ? $argv['id'] : 'ui-module';
	}

	function DisplayHeader() { ?>
<div class="ui-module" id="<?php echo $this->msId;?>">
<div class="ui-moduleHd" id="<?php echo $this->msId;?>">
	<?php echo $this->msTitle; ?>
</div>
<div class="ui-moduleBd" id="<?php echo $this->msId;?>">
	<div class="ui-moduleBdMain"><?php
	}

	function DisplayFooter() { ?>
	</div>
</div>
</div><?php
	}
}

class cDycmsClientModule extends cUiModule
{

}

class cHtmlHyperlink extends cHtmlWrapperTag
{
	function __construct($uri, $text = null, $argv = array())
	{
	 	$argv['href'] = $uri;
		parent::__construct('a', $argv);
		if($text) $this->AddChild($text);
	}
}


class cUiTable extends cHtmlWrapperTag
/**
* @desc
* Represents a matrix of values, with each column having
* a header. Rows in the table are represented by this node's
* children.
*/
{
	protected $mCols;

	function __construct($cols = null, $argv = array())
	{
		$this->mCols = $cols;
		parent::__construct('table', $argv);
	}

	function DisplayBody() {
        if(is_array($this->mCols)) {
			echo '<tr>';

			foreach($this->mCols as $col) {
                if($col instanceof IDycmsDisplayable){
                    echo '<th>';
                    $col->Display();
                    echo '</th>';
                } else
				    echo "<th>$col</th>";
			};

			echo '</tr>';
		}

		$n = 0;
		foreach($this->mChildren as $row) {
			$n++;
			echo (($n & 1) ? '<tr class="oddRow">' : '<tr>');

			$row->Display();

			echo '</tr>';
		}
	}
}

class cUiTableRow extends cDycmsClientNode
{
	private $mzColSpan;
	
	function __construct($colSpans = array()){
		$this->mzColSpan = $colSpans;
	}
	
	function DisplayBody() {
		for($i=0; $i<count($this->mChildren); $i++) {
			if(isset($this->mzColSpan[$i]))
				echo '<td class="col-'.$i.'" colspan="'.$this->mzColSpan[$i].'">';
			else
				echo '<td class="col-'.$i.'">';
			$this->mChildren[$i]->Display();
			echo '</td>';
		}
	}
}

class cUiImage extends cDycmsClientNode
{
	protected $msPath;

	function __construct($path){
		$this->msPath = $path;
	}

	function DisplayBody(){

	}
}

class cUiIcon extends cDycmsClientNode
{
	protected $mName;
	protected $msLabel;

	function __construct($name, $lbl = '') {
		$this->mName = $name;
		$this->msLabel = $lbl;
	}

	function DisplayBody() {
		uiIcon($this->mName).$this->msLabel;
	}
}

class cUiForm extends cHtmlWrapperTag
{
	function __construct($argv = array('method' => 'POST'),
			$vzAttrib = array()) {
			
		//
		// Add default attributes
		//
		
		if(!isset($argv['method']))
			$argv['method'] = 'POST';
		if(!isset($argv['action']) || ($argv['action'] == ''))
			$argv['action'] = $_SERVER['REQUEST_URI'];
		
		parent::__construct('form', $argv);

        //
        // Save GET data
        //

        if(($argv['method'] == 'GET') && !$vzAttrib['noSave']){
            foreach((isset($vzAttrib['saveData']) ? $vzAttrib['saveData']
					: $_GET) as $key => $val){
                $this->AddChild(new cUiFormData($key, $val));
            }
        }
	}

	function DisplayBody() {
		foreach($this->mChildren as $node) {
			echo '<div class="ui-formItem">';

			if($lbl = $node->GetLabel())
				echo '<span class="ui-formItemLabel">'.$lbl.'</span>';

			$node->Display();
			echo '</div>';
		}
	}
}

class cUiFormItem extends cDycmsClientNode
{
	protected $msLabel;

	function __construct($lbl = '') {
		$this->msLabel = $lbl;
	}

	function GetLabel() {
		return $this->msLabel;
	}
}

class cUiSelection extends cUiFormItem
{
    protected $mId;
    protected $mzOpts;
    protected $mSelected;

    function __construct($argv, $zOpts, $lbl = null){
    	parent::__construct($lbl);
		
        $this->mId = $argv['name'];
        $this->mzOpts = $zOpts;
        $this->mSelected = $argv['value'];
    }

    function DisplayBody(){
        echo '<select name="'.$this->mId.'">';
        foreach($this->mzOpts as $val => $lbl){
            if($this->mSelected == $val)
                echo "<option value=\"$val\" selected='true'>$lbl</option>";
            else
                echo "<option value=\"$val\">$lbl</option>";
        };
        echo '</select>';
    }
}

class cUiCaptcha extends cDycmsClientNode
{
	protected $mId;
	protected $msCode;

	function __construct($id){
		$this->mId = $id;
		$this->msCode = $_SESSION['$uiCaptcha-'.$this->mId];
	}

	function CheckCode($code){
		return (substr($this->msCode, 15, 5) == $code);
	}

	function CreateCode(){
		$this->msCode = $_SESSION['$uiCaptcha-'.$this->mId] = md5(rand(0,0xFFFF));

		$image = ImageCreate(100,20);
		$white = imagecolorallocate($image, 255,255,255);
		$black = imagecolorallocate($image, 0,0,0);
		$grey = imagecolorallocate($image, 204,204,204);

		// Black BG
		imagefill($image, 0, 0, $black);

		// String in white
		imagestring($image, 3, 30, 3, $this->msCode, $white);

		// Lines
		imagerectangle($image,0,0,99,19,$grey);

		// Output the image file
		imagepng($image, ROOT.'public/tmp/uiCaptcha-'.substr($this->msCode, 25, 7));
	}

	function Dispose(){
		session_unregister('$uiCaptcha-'.$this->mId);
		unlink(ROOT.'public/tmp/uiCaptcha-'.substr($this->msCode, 25, 7));
	}

	function DisplayHeader(){

	}

	function DisplayFooter(){
	?><img src="/private/system/public/tmp/uiCaptcha-<?php echo(substr($this->msCode, 25, 7)); ?>" /><?php
	}
}

class cUiButton extends cHtmlWrapperTag
{
	function __construct($argv, $text = null, $icon = null) {
		if(!isset($argv['type']))
			$argv['type'] = 'submit';
		
		// Hacks for IE
		$argv['onClick'] = 
			'ieButtonHack(this, \''.$argv['value'].'\')';
		if(isset($argv['class']))
			$argv['class'] .= ' buttonVal-'.$argv['value'];
		else
			$argv['class'] = 'buttonVal-'.$argv['value'];
		
		
		parent::__construct('button', $argv);

		if($icon) {
			$this->AddChild(new cUiIcon($icon));
		};
		if($text) {
			$this->AddChild($text);
		};
	}
}

class cUiButtonSet extends cDycmsClientNode
{
	function __construct($argv, $zButtons){
		foreach($zButtons as $value => $text){
			$argv['value'] = $value;
			$this->AddChild(new cUiButton($argv, $text));
		};		
	}
}	

class cUiInput extends cHtmlWrapperTag
{
	protected $msLabel;

	function __construct($argv = null, $label = null) {
		$this->msLabel = $label;
		parent::__construct('input', $argv);
	}

	function GetLabel() {
		return $this->msLabel;
	}
}

class cUiFormData extends cUiInput
{
    function __construct($name, $value) {
		parent::__construct(array(
            'type'=>'hidden',
		    'name'=>$name,
            'value'=>$value
		));
	}
}

class cUiTextBox extends cUiInput
{
	function __construct($argv = array(), $lbl = null, $pass = false) {
		if($pass)
			$argv['type'] = 'password';
		else
			$argv['type'] = 'text';

		parent::__construct($argv, $lbl);
	}
}

class cUiTextArea extends cHtmlWrapperTag
{
	protected $msLabel;

	function __construct($argv, $s_label = null)
	{
		parent::__construct('textarea', $argv);
		$this->msLabel = $s_label;
	}
	
	function GetLabel() {
		return $this->msLabel;
	}
}

class cUiCheckbox extends cUiInput
{
	function __construct($argv = array(), $lbl = null) {
		$argv['type'] = 'checkbox';

		parent::__construct($argv, $lbl);
	}
}

class cUiCollection extends cHtmlWrapper
{
	function __construct($s_name)
	{
		parent::__construct('<h3>'.uiGetIcon('info').
			"$s_name</h3><table class=\"collection\">",
			"</table>");
	}
}

class cUiCollectionSubCollection extends cDycmsClientNode
{
	protected $msName;
	protected $msUri;

	function __construct($s_name, $vz_criteria)
	{
		$this->msUri = dyBrowserUrl($vz_criteria);
		$this->msName = $s_name;
	}

	function DisplayHeader()
	{
		?>
<tr><td>
<a href="<?php echo($this->msUri) ?>"><?php echo(uiGetIcon('folder').$this->msName); ?></a>
<?php
	}

	function DisplayFooter()
	{
		?></td></tr><?php
	}
}

class cUiCollectionObject extends cDycmsClientNode
{
	protected $msName;
	protected $msUri;

	function __construct($obj)
	{
		$this->msUri = dyViewerUrl($obj->GetClsId(), $obj->GetId());
		$this->msName = $obj->GetSummary();
	}

	function DisplayHeader()
	{
		?>
<tr><td>
<a href="<?php echo($this->msUri) ?>"><?php echo(uiGetIcon('report').$this->msName); ?></a>
<?php
	}

	function DisplayFooter()
	{
		?></td></tr><?php
	}
}

class cUiException extends cDycmsClientNode
{
	protected $msMessage;
	protected $miCode;

	function __construct(Exception $e)
	{
		$this->msMessage = $e->getMessage();
		$this->miCode = $e->getCode();
		
		cUiPage::GetInst()->AddMetaData('resultContainer','');
		cUiPage::GetInst()->AddMetaData('status','Exception');
	}

	function Display()
	{
		echo uiErrorCode($this->miCode, $this->msMessage);
	}
}

include_once("page.inc.php");
include_once('admin.inc.php');

?>
